package cn.merryyou.dto.system.userDetails;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Objects;

public class UserDetailsImpl implements UserDetails {

    private Collection<SimpleGrantedAuthority> authorities;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;
    private String username;
    private String password;
    private String id;

    private UserDetailsImpl() {
    }

    public UserDetailsImpl(
            String id,
            String username,
            String password,
            Collection<SimpleGrantedAuthority> authorities,
            boolean accountNonExpired,
            boolean accountNonLocked,
            boolean credentialsNonExpired,
            boolean enabled) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.accountNonExpired = accountNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.credentialsNonExpired = credentialsNonExpired;
        this.enabled = enabled;
    }



    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof UserDetailsImpl)) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }
        UserDetailsImpl that = (UserDetailsImpl) o;
        return accountNonExpired == that.accountNonExpired
                && accountNonLocked == that.accountNonLocked
                && credentialsNonExpired == that.credentialsNonExpired
                && enabled == that.enabled
                && Objects.equals(authorities, that.authorities);
    }

    @Override
    public int hashCode() {

        return Objects.hash(
                super.hashCode(),
                authorities,
                accountNonExpired,
                accountNonLocked,
                credentialsNonExpired,
                enabled);
    }
}
